<template>
	<view :style="viewColor">
		<div class="detop">
			<image :src="imgHost + '/index/denlu.png'" mode=""></image>
			<p class="yuang">创建企业名片</p>
			<div class="qiye">
				<div @click="myisbos">我是BOSS</div>
				<div @click="myissta">我是员工</div>
			</div>
		</div>
		<div class="decen">
			<div></div>
			<span>或者</span>
		</div>
		<div class="detop">
			<image :src="imgHost + '/index/denluo.png'" mode=""></image>
			<p class="yuang">我没有企业，想创建个人名片</p>
			<div @click="soloCard">我的名片</div>
		</div>
	</view>
</template>

<script>
	import {
		HTTP_REQUEST_URL
	} from '@/config/app.js';
	import {
		mapGetters
	} from 'vuex';
	import {
		enterpriseSpread,
		enterpriseSpreadPay,
		checkPay
	} from '@/api/user.js'
	import {
		orderPay
	} from '@/api/order.js';
	export default {
		data() {
			return {
				imgHost: HTTP_REQUEST_URL + '/mingpian',
				enable: '',
				// pay_close:true,
				isPay: false,
				timer:""
			};
		},
		computed: {
			...mapGetters(['viewColor']),
		},
		onShareAppMessage(e) {
			return { // 自定义分享内容
				title: '创建名片',
				path: '/pages/userminp/denlu/denlu'
			}
		},
		onShareTimeline() {
			return {
				title: '创建名片',
				path: '/pages/userminp/denlu/denlu'
			};
		},
		onLoad(options) {
			this.enable = options.enable
			this.settime()
		},
		mounted() {},
		methods: {
			settime(){
				this.timer = setInterval(this.FcheckPay, 1000)
			},
			FcheckPay() {
				checkPay().then(res => {
					console.log(res, 'checkPay');
					this.isPay = res.message == 0 ? false : true
					if (this.isPay) {
						clearInterval(this.timer)
					}
					console.log(this.isPay);
				})
			},
			myisbos() {
				let that = this
				if (this.enable == 1) {
					enterpriseSpread().then(res => {
						if (res.status == 200) {
							if(!this.isPay){
								uni.showModal({
									title: `开通企业需支付${res.data.Settle_pay}`,
									content: '确认支付吗？',
									success: function(rec) {
										if (rec.confirm) {
											console.log('点击了确认')
											if (res.data.enable_enterprise_spread != 0) {
												// if (true) {
												checkPay().then(rea => {
													if (rea.message == 0) {
														enterpriseSpreadPay({
															pay_type: 'routine'
														}).then(req => {
															that.callPay(req);
															if (that.isPay) {
																uni.hideLoading();
																return that.$util.Tips({
																	title: '开通成功',
																}, () => {
																	clearInterval(that.timer)
																	uni.navigateTo({
																		url: '/pages/userminp/myisboss/myisboss'
																	})
																});
															} else {
																that.callPay(req);
															}
														}).catch(err => {
															uni.hideLoading();
															uni.showToast({
																title: err,
																icon: 'none'
															});
														});
													} else {
														clearInterval(that.timer)
														uni.navigateTo({
															url: '/pages/userminp/myisboss/myisboss'
														})
													}
												})
											}
										} else {
											console.log('点击了取消')
											uni.showToast({
												title: '取消支付',
												icon: 'none',
												duration: 2000
											})
											return false
										}
									}
								})
							}else{
								clearInterval(that.timer)
								uni.navigateTo({
									url: '/pages/userminp/myisboss/myisboss'
								})
							}
						}
					})
				}else{
					clearInterval(this.timer)
					uni.navigateTo({
						url: '/pages/userminp/myisboss/myisboss'
					})
				}
			},
			// 调用支付
			callPay(res) {
				let that = this;
				let status = res.data.status,
					orderId = res.data.result.order_id,
					callback_key = res.data.result.pay_key,
					jsConfig = res.data.result.config,
					goPages = '/pages/annx/vip_center/index'
				switch (status) {
					case 'ORDER_EXIST':
					case 'EXTEND_ORDER':
					case 'PAY_ERROR':
					case 'error':
						uni.hideLoading();
						// that.payClose();
						return that.$util.Tips({
							title: res.message
						});
						break;
					case 'success':
						uni.hideLoading();
						// that.payClose();
						return that.$util.Tips({
							title: res.message,
							icon: 'success'
						});
						break;
					case 'alipay':
					case "alipayQr":
						uni.hideLoading();
						// that.payClose();
						uni.navigateTo({
							url: '/pages/order_pay_back/index?keyCode=' + callback_key + '&url=' + jsConfig,
						})
						return;
						break;
						// #ifndef MP
					case "wechat":
					case "weixin":
					case "weixinApp":
						jsConfig.timeStamp = jsConfig.timestamp;
						// #ifndef APP-PLUS
						this.$wechat.pay(jsConfig).then(res => {
							uni.hideLoading();
							// that.payClose();
							return that.$util.Tips({
								title: res.message,
								icon: 'success'
							}, {
								tab: 4,
								url: goPages
							});
						}).catch(res => {
							uni.hideLoading();
							// that.payClose();
							if (res.errMsg == 'chooseWXPay:cancel') return that.$util.Tips({
								title: '取消支付'
							}, {
								tab: 5,
								url: goPages + '&status=0'
							});
						})
						// #endif
						// #ifdef APP-PLUS
						let mp_pay_name = ''
						if (uni.requestOrderPayment) {
							mp_pay_name = 'requestOrderPayment'
						} else {
							mp_pay_name = 'requestPayment'
						}
						uni[mp_pay_name]({
							provider: 'wxpay',
							orderInfo: jsConfig,
							success: (e) => {
								uni.hideLoading();
								// that.payClose();
								let url = "/pages/annx/vip_center/index"
								return that.$util.Tips({
									title: '支付成功',
									icon: 'success'
								}, {
									tab: 4,
									url: url
								});
							},
							fail: (e) => {
								uni.hideLoading();
								// that.payClose();
								uni.showModal({
									content: "支付失败",
									showCancel: false,
									success: function(res) {}
								})
							},
							complete: () => {
								uni.hideLoading();
							},
						});
						// #endif
						break;
						// #endif
						// #ifdef MP
					case "routine":
						jsConfig.timeStamp = jsConfig.timestamp;
						let mp_pay_name = ''
						if (uni.requestOrderPayment) {
							mp_pay_name = 'requestOrderPayment'
						} else {
							mp_pay_name = 'requestPayment'
						}
						uni[mp_pay_name]({
							...jsConfig,
							success: function(res) {
								uni.hideLoading();
								// that.payClose();
								return that.$util.Tips({
									title: '支付成功',
									icon: 'success'
								}, {
									tab: 5,
									url: goPages
								});
							},
							fail: function(e) {
								uni.hideLoading();
								// that.payClose();
								return that.$util.Tips({
									title: '取消支付'
								});
							},
						})
						break;
						// #endif
					case "balance":
						uni.hideLoading();
						// that.payClose();
						//余额不足
						return that.$util.Tips({
							title: res.msg
						}, {
							tab: 5,
							url: goPages
						});
						break;
						// #ifdef H5
					case 'h5':
						let host = window.location.protocol + "//" + window.location.host;
						let url = `${host}/pages/annex/vip_paid/index`
						let eUrl = encodeURIComponent(url)
						let jsurl = jsConfig.mweb_url || jsConfig.h5_url
						let locations = `${jsurl}&redirect_url=${eUrl}`
						setTimeout(() => {
							location.href = locations;
						}, 100);
						break;
						// #endif
						// #ifdef APP-PLUS
					case 'alipayApp':
						uni.requestPayment({
							provider: 'alipay',
							orderInfo: jsConfig,
							success: (e) => {
								uni.hideLoading();
								that.payClose();
								return that.$util.Tips({
									title: '支付成功',
									icon: 'success'
								}, {
									tab: 5,
									url: goPages
								});
							},
							fail: (e) => {
								uni.hideLoading();
								uni.showModal({
									content: "支付失败",
									showCancel: false,
									success: function(res) {}
								})
							},
							complete: () => {
								uni.hideLoading();
							},
						});
						break;
						// #endif
				}
			},
			myissta() {
				clearInterval(this.timer)
				uni.navigateTo({
					url: '/pages/userminp/myisv/myisv'
				})
			},
			soloCard() {
				clearInterval(this.timer)
				uni.navigateTo({
					url: '/pages/userminp/soloCard/soloCard'
				})
			}
		}
	}
</script>

<style lang="scss">
	.decen {
		position: relative;
		top: 90rpx;
		padding: 0rpx 30rpx;

		div {
			height: 3rpx;
			background-color: #999;
		}

		span {
			display: inline-block;
			background-color: #f5f5f5;
			position: absolute;
			top: -20rpx;
			left: 50%;
			margin-left: -28rpx;
			font-weight: 400;
			font-size: 28rpx;
			line-height: 44rpx;
			text-align: left;
			color: #999;
		}
	}

	.detop {
		padding-top: 160rpx;
		margin: auto;
		text-align: center;

		image {
			width: 338rpx;
			height: 255.04rpx;
		}

		.yuang {
			margin-top: 40rpx;
			margin-bottom: 40rpx;
			font-weight: 700;
			font-size: 36rpx;
			line-height: 40rpx;
			color: #000;
		}

		div {
			margin: auto;
			width: 464rpx;
			height: 96rpx;
			border-radius: 24rpx;
			background: var(--view-theme);
			box-shadow: 0 12rpx 36rpx rgba(193, 0, 0, 0.1);
			font-weight: 600;
			font-size: 34rpx;
			line-height: 96rpx;
			text-align: center;
			color: #fff;
		}

		.qiye {
			display: flex;
			background-color: transparent;

			&>div {
				width: 50%;
				margin: 0 10rpx;
			}
		}
	}
</style>